Zepto(function($) {
  var title = sessionStorage.getItem("cateName");
  var cateId = sessionStorage.getItem("cateId");
  $("title").html(title);
  $("#modelTitleName").html(title);
  window.onload = function () {
    categary();
    initList();
  }
  /**
   * 上拉加载具体业务实现
   */
  var next = 0;
  // function lowEnough(){
  //   var pageHeight = Math.max(document.body.scrollHeight,document.body.offsetHeight);
  //   var viewportHeight = window.innerHeight ||
  //       document.documentElement.clientHeight ||
  //       document.body.clientHeight || 0;
  //   var scrollHeight = window.pageYOffset ||
  //       document.documentElement.scrollTop ||
  //       document.body.scrollTop || 0;
  //   return pageHeight - viewportHeight - scrollHeight < 20;
  // }
  // function doSomething(){
  //   initList();
  //   pollScroll();//继续循环
  //   $('#spinner').hide();
  // }
  // function checkScroll(){
  //   if(!lowEnough()) return pollScroll();
  //   $('#spinner').show();
  //   setTimeout(doSomething,1000);
  // }
  // function pollScroll(){
  //   setTimeout(checkScroll,1000);
  // }
  function initList() {
    $.ajax({
      type: 'GET',
      url: getUrl + '/wxmall/goods/selectCategoryGoodsList',
      data: {
        "categoryId":cateId,
        "current": next,
        "limit": 2
      },
      dataType: 'json',
      timeout: 15000,
      headers: headers("goods.selectCategoryGoodsList"),
      context: $('body'),
      success: function(data){
        goodsinitList(data);
      },
      error: function(xhr, type){
        // alert('Ajax error!')
      }
    })
  }
  $("#spinner").click(function () {
    initList();
  });
  /**
   * 初始化商品列表
   *
   * @param data
   */
  function goodsinitList(data) {
    console.log(data);
    if(!data.success){
      return
    }
    var data2 = data.data;
    var data1 = data2.list;
    next = data2.next;
    var table = document.body.querySelector('.mui-table-view1');
    if(data2.listSize == 0){
      $('#spinner')[0].innerHTML="亲~没有更多商品了";
      setTimeout(function () {
        $('#spinner').hide();
      },1000);
    }
    if(data1 != null && data2 != undefined && data2 != ""){
      for(var i = 0; i < data1.length; i++){
        var goodsName = "";
        if(data1[i].goodsName != null){
          goodsName = data1[i].goodsName;
        }
        var wechat_price = "";
        if(data1[i].wechat_price != null){
          wechat_price ="￥" + data1[i].wechat_price;
        }
        var limitPrice = "";
        if(data1[i].limitPrice != null){
          limitPrice ="￥" + data1[i].limitPrice;
        }
        var goodsId = "";
        if(data1[i].goodsId != null){
          goodsId =data1[i].goodsId;
        }
        var goodsImage = ""
        if(data1[i].goodsImage != null && data1[i].goodsImage.length != 0){
          if(data1[i].goodsImage[0].picUrl != null){
            goodsImage = imgUrl +  data1[i].goodsImage[0].picUrl;
          }
        }

        var li = document.createElement('li');
        li.setAttribute("data-goodId",goodsId);
        li.innerHTML= '<div class="e-video">'+
            '<img src="'+ goodsImage +'">'+
            '</div>'+
            '<div class="specifica-goods">'+
            '<div class="goods-title">'+goodsName+'</div>'+
            '<div class="goods-detail">'+
            '<div class="goods-price">'+
            '<span class="reality-goods-price">'+ wechat_price +'</span>'+
            '<span class="original-goods-price">'+limitPrice+'</span>'+
            '<span class="original-goods-price1"></span>'+
            '</div>'+
            '<div class="add-icon">'+
            '<div class="add-cart"></div>'+
            '<div class="selec-details"></div>'+
            '</div>'+
            '</div>'+
            '</div>';
            table.appendChild(li);
        // html += '<li data-goodId="'+goodsId+'">'+
        // '<div class="e-video">'+
        // '<img src="'+ goodsImage +'">'+
        // '</div>'+
        // '<div class="specifica-goods">'+
        // '<div class="goods-title">'+goodsName+'</div>'+
        // '<div class="goods-detail">'+
        // '<div class="goods-price">'+
        // '<span class="reality-goods-price">'+ wechat_price +'</span>'+
        // '<span class="original-goods-price">'+limitPrice+'</span>'+
        // '<span class="original-goods-price1"></span>'+
        // '</div>'+
        // '<div class="add-icon">'+
        // '<div class="add-cart"></div>'+
        // '<div class="selec-details"></div>'+
        // '</div>'+
        // '</div>'+
        // '</div>'+
        // '</li>';
      }
      // $("#goodsList").append(html);
    }
  }

  function categary() {
    $.ajax({
      type: 'GET',
      url: getUrl + '/wxmall/categorygoods/list',
      data: {},
      dataType: 'json',
      timeout: 15000,
      headers: headers("categorygoods.list"),
      context: $('body'),
      success: function(data){
        categaryInit(data);
      },
      error: function(xhr, type){
      }
    })
  }

  /**
   * 初始化类目列表
   *
   * @param data
   */
  function categaryInit(data) {
    $(".categary").html("");
    if(!data.success){
      return
    }
    var data1 = data.data;
    if(data1 != null && data1 != undefined && data1 != ""){
      var html = "";
      for(var i = 0; i < data1.length; i++){
        var cateName = "";
        if(data1[i].cateName != null){
          cateName = data1[i].cateName;
        }
        if(data1[i].cateName === title){
          html += '<div data-cata="'+data1[i].cateId+'" class="swiper-slide active">'+cateName+'</div>';
        }else {
          html += '<div data-cata="'+data1[i].cateId+'" class="swiper-slide">'+cateName+'</div>';
        }
      }
      $("#categary").append(html);
    }
    //初始化类目列表
    var swiper = new Swiper('.swiper-container', {
      pagination: '.swiper-pagination',
      slidesPerView: 5,
      paginationClickable: true,
      spaceBetween: 5
    });
  }

  /**
   * tab 切换
   *
   */
  $(".tab").on("click","div",function () {
    $(".tab>div").removeClass("active");
    $(this).addClass('active');
  });
  $(".tab").on("click",".tab1",function () {
    $(".tab1-cont").css("display","block");
    $(".tab2-cont").css("display","none");
    $(".tab3-cont").css("display","none");
    window.location.href = "/index/index.html"
  });
  $(".tab").on("click",".tab2",function () {
    $(".tab1-cont").css("display","none");
    $(".tab2-cont").css("display","block");
    $(".tab3-cont").css("display","none");
  });
  $(".tab").on("click",".tab3",function () {
    $(".tab1-cont").css("display","none");
    $(".tab2-cont").css("display","none");
    $(".tab3-cont").css("display","block");
  });
  /**
   * 跳转商品详情
   *
   */
  $("ul").on("click","li",function () {
    sessionStorage.setItem("brandGoodId", $(this)[0].getAttribute("data-goodId"));
    window.location.href = "/index/bgupGoodsDetails.html"
  });
  // 选择类目
  $(".goods-model").on("click",".swiper-slide",function () {
    $(".title-name").html($(this).html());
    $("title").html($(this).html());
    $(".swiper-slide").removeClass("active");
    $(this).addClass("active");
    cateId = $(this)[0].getAttribute("data-cata");
    next = 0;
    $("#goodsList").html("");
    initList();
    $('#spinner').show();
    $('#spinner')[0].innerHTML="点击加载更多";
  });
});